package com.example.administrator.officeplatform.kj;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.administrator.officeplatform.MyApplication;
import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.adapter.PersonAdapter;
import com.example.administrator.officeplatform.adapter.YCBFYAdapter;
import com.example.administrator.officeplatform.base.BaseActivity;
import com.example.administrator.officeplatform.bean.LoanInfo;
import com.example.administrator.officeplatform.bean.PersonInfo;
import com.example.administrator.officeplatform.bean.ReimbursementVoInfo;
import com.example.administrator.officeplatform.bean.contentListInfo;
import com.example.administrator.officeplatform.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class YCBFYActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.rv)
    RecyclerView rv;
    private Context mContext;
    private List<RelativeLayout> ls_rl;
    private List<View> ls_v;
    private List<ReimbursementVoInfo> ls_audit = new ArrayList<>();
    private YCBFYAdapter personAdapter;
    private OkHttpClient mOkHttpClient;
    private String newtext = "";
    private String index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_ycbfy);

    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mContext=this;
        //设置布局管理器
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setHasFixedSize(true);
        rv.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    protected void initData() {
        index=(String)getIntent().getExtras().get("index");
        ls_audit= new ArrayList<>();
        search();

    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void search(){

        mOkHttpClient = new OkHttpClient();
        Map<String, Object> map = new HashMap<>();
        map.put("reimbursement", newtext);
        String json = JSON.toJSONString(map);
        RequestBody formBody =  FormBody.create(MediaType.parse("application/json;charset=UTF-8"),json);
        Request request = new Request.Builder()
                .url("http://192.168.5.19:7676/reimbursement/expense/seachExpenseCost")
                .header("cookie", MyApplication.getMsession())
                .addHeader("Content-Type","application/json;charset=UTF-8")
                .post(formBody)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mContext, "连接异常", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                if(str.indexOf("contentList")>=0){
                    String Msg = JsonUtil.getMsg(str,"contentList");
                    String Msg1 = JsonUtil.getMsg(str,"contentList");
                    final List<ReimbursementVoInfo> lsMap = JsonUtil.getPersons(Msg, new TypeToken<List<ReimbursementVoInfo>>() {
                    }.getType());
                    ls_audit.addAll(lsMap);
//                    if ("success".equals(Msg)){
//
//                    }
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(ls_audit.size()==0){
                            Toast.makeText(mContext, "未查询到信息", Toast.LENGTH_SHORT).show();
                        }

                        if(personAdapter==null){
                            personAdapter=new YCBFYAdapter(ls_audit,mContext);
                            rv.setAdapter(personAdapter);
                        }else{
                            personAdapter.notifyDataSetChanged();
                        }
                        personAdapter.setOnRecyclerViewListener(new YCBFYAdapter.OnRecyclerViewListener() {
                            @Override
                            public void onItemClick(int position) {
                                Intent mIntent = new Intent();
                                ReimbursementVoInfo bmInfo = ls_audit.get(position);
                                //把返回数据存入Intent
                                mIntent.putExtra("index", index);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("loan", bmInfo);
                                mIntent.putExtras(bundle);
                                //设置返回数据
                                setResult(2, mIntent);
                                finish();
                            }

                            @Override
                            public boolean onItemLongClick(int position) {
                                return false;
                            }

                        });
                    }
                });
            }

        });


    }
}
